<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.stabilityPerson_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/xapply/stability/stabilityPerson";
  import {mapGetters} from "vuex";
  import { validatenull,isvalidatemobile,cardid,isPhone} from "@/util/validate";
  export default {
    data() {
      //手机号校验非空
      const validatePhone = (rule, value, callback) => {
        if(!validatenull(value)){
            if (isvalidatemobile(value)[0]) {
              callback(new Error(isvalidatemobile(value)[1]));
            } else {
              callback();
            }
          }else{
            callback();
          }
      };  
       const validateTelePhone = (rule, value, callback) => {
        console.log(value);
        if(!validatenull(value)){
          if (!isPhone(value)) {
            callback("请输入正确的固定电话号码");
          } else {
            callback();
          }
        }else{
            callback();
        }
      };
      //校验身份正号码
      const validateCardid = (rule, value, callback) => {
        if(cardid(value)[0]){
          callback(new Error(cardid(value)[1]))
        }else{
          callback();
        }
      };
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          indexLabel:'序号',
          height: 'auto',
          calcHeight: 30,
          dialogWidth: 1050,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          excelBtn: true,
          dialogClickModal: false,
          column: [
            {
              label: "主键id",
              prop: "id",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
           {
              label: "所属网格",
              prop: "gridId",
              type: "tree",
              span: 24,
              //multiple: true,
              parent: true,
              display: true,
              search: true,
              dicMethod: "get",
              dicUrl: "/api/smart-system/dept/treeByPid",
              props: {
                label: "title"
              
              },
              rules: [{
                required: true,
                message: "请输入所属网格",
                trigger: "blur"
              }],
           },
            {
              label: "姓名",
              prop: "name",
              type: "input",
              search: true,
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }],
            },
            {
              label: "身份证号",
              prop: "idCardNo",
              type: "input",
              search: true,
              rules: [{
                required: true,
                validator: validateCardid,
                trigger: "blur"
              }],
            },
            {
              label: "人员类型",
              prop: "stabilityType",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=types_of_key_personnel",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              rules: [{
                required: true,
                message: "请输入人员类型",
                trigger: "blur"
              }],
            },
           
            {
              label: "网格名称",
              prop: "gridName",
              type: "input",
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
              hide: true,
            },
             {
              label: "手机号",
              prop: "mobilePhone",
              type: "input",
              rules: [{
                validator: validatePhone,
                trigger: "blur"
              }],
            },
            {
              label: "联系电话",
              prop: "telephone",
              type: "input",
              hide: true,
              rules: [{
                validator: validateTelePhone,
                trigger: "blur"
              }],
            },
           
            {
              label: "性别",
              prop: "sex",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=sex",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入性别",
                trigger: "blur"
              }],
            },
            {
              label: "教育程度",
              prop: "education",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=education",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "宗教",
              prop: "regilion",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=religion",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "婚姻状况",
              prop: "marriage",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=marital_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "民族",
              prop: "nation",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=nation",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "工作地点",
              prop: "workPlace",
              type: "input",
            },
            {
              label: "职务",
              prop: "job",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=sys_user_post",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
            },
            {
              label: "户籍地",
              prop: "hometown",
              type: "input",
              span: 24,
              hide: true,
            },
            {
              label: "家庭住址",
              prop: "homeAddress",
              type: "input",
              span: 24,
              hide: true,
            },
            {
              label: "备注",
              prop: "remark",
              type: "textarea",
              span: 24,
              hide: true,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.stabilityPerson_add, false),
          viewBtn: this.vaildData(this.permission.stabilityPerson_view, false),
          delBtn: this.vaildData(this.permission.stabilityPerson_delete, false),
          editBtn: this.vaildData(this.permission.stabilityPerson_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
          //新增和修改页面，控制网格树加载职能选择末级，查询页面可以选择父级
        if (["edit", "add"].includes(type)) {
            const gridId = this.findObject(this.option.column,"gridId");
            gridId.parent = false;
           
        }
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
